import 'package:app/common/colors.dart';
import 'package:app/pages/sign_in_module/sign_bottom_controller_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:app/utils/persisten_storage.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../services/dio_manager.dart';
import '../account_security_module/account_security_view.dart';
import '../login_modules/login_view.dart';
import '../userinfo_module/personal_files_module/personal_files_view.dart';
import '../userinfo_module/userinfo_model.dart';
import 'mine_aboutus_manage.dart';
import 'mine_face_manage.dart';
import 'mine_faces_manage.dart';
import 'mine_feedback_report.dart';
import 'mine_security.dart';
import 'serve_setting_page.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin{
//授权页登录业务逻辑添加
  String? loginName;
  String login_mobile = "";
  String login_acctoken = "";
  String? headerImg;

  @override
  void initState() {
    super.initState();
    requestDataWithUserInfo();
    // print("MinePage==initState>>");
    // getInitData();
  }

  UserinfoModel? model;

  requestDataWithUserInfo()async{

    var json = await DioManager().kkRequest(Address.infoRead, method: 'get');

    UserinfoModel _model = UserinfoModel.fromJson(json);

    model = _model;

    loginName = model?.data?.name;
    headerImg = '${Address.homeHost}${model?.data?.image_128}';
    setState(() {

    });
  }

  // void getInitData() async {
  //   print("getInitData==print>>");
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   login_name = prefs.getString("name")!;
  //   login_mobile = prefs.getString("login_mobile")!;
  //   login_image_128 = prefs.getString("image128")!;
  //   print("login_name==$login_name>>");
  //   print("login_mobile==$login_mobile>>");
  //   if (login_image_128.startsWith("http")) {
  //     print("login_image_128==$login_image_128>>");
  //   } else {
  //     login_image_128 = Address.homeHost + login_image_128;
  //     print("login_image_128==$login_image_128>>");
  //   }
  //   setState(() {});
  // }

  List menuTitles = [
    '实名认证',
    '定位',
    '账号安全',
    '帮助与反馈',
    '关于',
    '通用',
  ];
  List menuIcons = [
    "images/mine_icons0.png",
    "images/mine_icons1.png",
    "images/mine_icons2.png",
    "images/mine_icons3.png",
    "images/mine_icons4.png",
    "images/mine_icons5.png",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.separated(
          itemBuilder: (context, index) {
            if (index == 0) {
              return Column(
                children: [
                  Padding(
                      padding: const EdgeInsets.all(8),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            margin: const EdgeInsets.only(left: 12),
                            child: const Text(
                              '我的',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                          ),
                          IconButton(
                            icon: Image.asset("images/mine_replace.png"),
                            onPressed: () {
                              Get.offAll(LoginView());
                              // 修改 state 内部变量, 且需要界面内容更新, 需要使用 setState()
                              // setState(() {});
                            },
                          )
                        ],
                      )),
                  GestureDetector(
                      onTap: () {
                        Get.toNamed(AppRoutes.userInfoView);
                      },
                      child: Container(
                        height: 100,
                          decoration: const BoxDecoration(
                            // color: Colors.red
                          ),
                          child: Padding(
                              padding: const EdgeInsets.only(right: 15,left: 15),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      Container(
                                        clipBehavior: Clip.antiAlias,
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(Radius.circular(25))
                                        ),
                                        child:  CachedNetworkImage(
                                          fit: BoxFit.cover,
                                          width: 50,
                                          height: 50,
                                          imageUrl: "${headerImg}",
                                          placeholder: (context, url) => CircularProgressIndicator(),
                                          errorWidget: (context, url, error) => Icon(Icons.error),
                                      ),),
                                      Container(
                                        margin: EdgeInsets.only(left: 10),
                                        child: Text('${loginName}'),
                                      )
                                    ],
                                  ),
                                  Container(
                                    child:  Icon(
                                      Icons.arrow_forward_ios,
                                      size: 15,
                                      color: AppColor.smallTextColor,
                                    ),
                                  ),
                                ],
                              ))))
                ],
              );
            }
            index -= 1;
            return GestureDetector(
                onTap: () {
                  print("index==>>>$index==>>");
                  if (index == 0) {
                    //实名认证
                    // Get.to(ManageFacesPage());
                    // Get.to(PersonalFilesView());
                    Get.toNamed(AppRoutes.hrEmployeeRead);
                  } else if (index == 1) {
                    //签到
                    Get.to(SignBottomControllerView());
                  } else if (index == 2) {
                    //账号安全
                    Get.to(MineSecurityPage());
                  } else if (index == 3) {
                    //帮助与反馈
                    Get.to(ManageFeedBackPage());
                  } else if (index == 4) {
                    //关于
                    Get.to(ManageAboutUs());
                  } else if (index == 5) {
                    //通用
                    // Get.toNamed(AppRoutes.accountSecurity);
                    //设置
                    Get.toNamed(AppRoutes.allGrid);
                  }
                },
                child: Container(
                    child: ListTile(
                  leading: Image.asset(menuIcons[index], width: 32, height: 32),
                  //左图标
                  title: Text(
                    menuTitles[index],
                    style: const TextStyle(fontSize: 16, color: Colors.black),
                  ),
                  //中间标题
                  trailing: const Icon(
                    Icons.arrow_forward_ios,
                    size: 15,
                  ),
                )));
          },
          separatorBuilder: (context, index) {
            return (index == 0 || index == 3)
                ? const Divider(thickness: 16, color: Color(0xFFEEEEEE))
                : const Divider(indent: 20, endIndent: 20);
          }, //分隔线
          itemCount: menuTitles.length + 1),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
